﻿<%@ Page Culture="auto" UICulture="auto" Title="Determinacy constraints" Language="C#"
    MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Determinancy.aspx.cs"
    Inherits="BM.Tools.WebControls.MForm.Example.Constraints.Determinancy" %>

<%@ Register Assembly="BM.Tools.WebControls.MForm" Namespace="BM.Tools.WebControls.MForm.Controls"
    TagPrefix="mf" %>
<%@ Register Assembly="BM.Tools.WebControls.MForm" Namespace="BM.Tools.WebControls.MForm.Controls.ValueHandlers"
    TagPrefix="mfvh" %>
<%@ Register Assembly="BM.Tools.WebControls.MForm" Namespace="BM.Tools.WebControls.MForm.Controls.Additions"
    TagPrefix="mfadd" %>
<%@ Register Assembly="BM.Tools.WebControls.MForm" Namespace="BM.Tools.WebControls.MForm.Controls.Additions.Actions"
    TagPrefix="mfac" %>
<asp:Content ContentPlaceHolderID="Demonstration" runat="server">
    <p>
        The demonstration below shows an imaginary doctor's visit element.
    </p>
    <p>
        The visit may be the first one, or it may be a consecutive visit:
    </p>
    <ul>
        <li>If it is the first visit, the <code>previous visit date</code> field should be hidden.
        </li>
        <li>If it is not the first visit, the <code>previous visit date</code> field should
            be visible. </li>
        <li>If it is the first visit, it is also impossible that the prescription is the same
            as the previous one, so the value of this field should be set to false and disabled.
        </li>
        <li>If it is not the first visit, we have to ensure that the field should be enabled.
        </li>
    </ul>
    <p>
        . However, if it is a consecutive visit and the prescription is the same as the
        previous one, the <code>prescription</code> field should be hidden.
    </p>
    <mf:RootValidator runat="server" ControlToValidate="Root1" />
    <mf:Root ID="Root1" runat="server" Mode="Edit">
        <UriMappings>
            <mf:UriMapping Prefix="" Namespace="" />
        </UriMappings>
        <Contents>
            <mf:Branch Name="DoctorVisit" runat="server">
                <Contents>
                    <mf:Leaf Name="IsForTheFirstTime" runat="server" DataType="Boolean">
                        <ValueHandler>
                            <mfvh:DropDownListValueHandler runat="server">
                                <DropDownList>
                                    <asp:ListItem Value="true">#true#</asp:ListItem>
                                    <asp:ListItem Value="false">#false#</asp:ListItem>
                                </DropDownList>
                            </mfvh:DropDownListValueHandler>
                        </ValueHandler>
                        <Additions>
                            <mfadd:Determinacy runat="server" Operator="Equals" ValueToCompare="false">
                                <mfadd:Dependant Path="../PreviousVisitDate">
                                    <mfac:Hide Speed="slow" />
                                    <mfac:SetOptional />
                                </mfadd:Dependant>
                            </mfadd:Determinacy>
                            <mfadd:Determinacy runat="server" Operator="NotEquals" ValueToCompare="false">
                                <mfadd:Dependant Path="../PreviousVisitDate">
                                    <mfac:Show Speed="slow" />
                                    <mfac:SetRequired />
                                </mfadd:Dependant>
                            </mfadd:Determinacy>
                            <mfadd:Determinacy runat="server" ValueToCompare="true">
                                <mfadd:Dependant Path="../SamePrescriptionAsPreviously">
                                    <mfac:SetValue Value="false" />
                                    <mfac:Disable />
                                </mfadd:Dependant>
                            </mfadd:Determinacy>
                            <mfadd:Determinacy runat="server" ValueToCompare="false">
                                <mfadd:Dependant Path="../SamePrescriptionAsPreviously">
                                    <mfac:Enable />
                                </mfadd:Dependant>
                            </mfadd:Determinacy>
                        </Additions>
                    </mf:Leaf>
                    <mf:Leaf Name="PreviousVisitDate" runat="server" DataType="Date">
                    </mf:Leaf>
                    <mf:Leaf Name="SamePrescriptionAsPreviously" runat="server" DataType="Boolean">
                        <ValueHandler>
                            <mfvh:DropDownListValueHandler runat="server">
                                <DropDownList>
                                    <asp:ListItem Value="true">#true#</asp:ListItem>
                                    <asp:ListItem Value="false">#false#</asp:ListItem>
                                </DropDownList>
                            </mfvh:DropDownListValueHandler>
                        </ValueHandler>
                        <Additions>
                            <mfadd:Determinacy runat="server" ValueToCompare="true">
                                <mfadd:Dependant Path="../Prescription">
                                    <mfac:SetOptional />
                                    <mfac:Hide />
                                </mfadd:Dependant>
                            </mfadd:Determinacy>
                            <mfadd:Determinacy runat="server" ValueToCompare="false">
                                <mfadd:Dependant Path="../Prescription">
                                    <mfac:SetRequired />
                                    <mfac:Show />
                                </mfadd:Dependant>
                            </mfadd:Determinacy>
                        </Additions>
                    </mf:Leaf>
                    <mf:Leaf Name="Prescription" runat="server">
                        <Additions>
                            <mfadd:Restriction runat="server" RestrictionType="MaxLength" Value="300"></mfadd:Restriction>
                        </Additions>
                    </mf:Leaf>
                </Contents>
            </mf:Branch>
        </Contents>
    </mf:Root>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="Description" runat="server">
    <p>
        The basic idea that lies behind the determinacy constraint is that a certain value
        or existence of one node can somehow affect a different node.
    </p>
    <p>
        The node that affects the other one is the determinant, whereas the one that is
        affected is called the dependant.
    </p>
    <p>
        The change that is executed on the dependant is called the <code>Action</code>.
        There are several different actions available. There is also a possibility to run
        a custom action.
    </p>
    <p>
        It depends on the determinacy existence or value, whether the action will be fired.
        A condition that is used can be created by properly setting a property <code>Operator</code>
        and a property <code>ValueToCompare</code>.
    </p>
    <ul>
        <li>If the <code>Operator</code>'s property value is <code>Exists</code> - the action
            is performed whenever the item exists.</li>
        <li>If the <code>Operator</code>'s property value is <code>NotExists</code> - the action
            is performed whenever the item does not exists.</li>
        <li>If the <code>Operator</code>'s property value is <code>Equals</code>, <code>NotEquals</code>,
            <code>GreaterThan</code>, <code>GreaterThanEqual</code>, <code>LessThan</code>,
            <code>LessThanEqual</code> - the action is performed whenever the comparison between
            the item's value and the <code>ValueToCompare</code> property using the given operator
            is true.</li>
    </ul>
    <p>
        The <code>ValueToCompare</code> property can also be provided as a list of alternative
        values (a | b | c). In such a situation only <code>Equals</code> and <code>NotEquals</code>
        operator values make sense. When the operator is set to <code>Equals</code>, then
        the action is fired if the item's value equals one of the values in the array. When
        the operator is set to <code>NotEquals</code>, then the action is fired if the item's
        value does not equal any of the values in the array.
    </p>
    <p>
        One determinacy may have one or more dependants. Each dependant can have one or
        more actions fired on itself if the determinant's condition matches. There are several
        predefined actions available. There is also a possibility to write a custom action
    </p>
    <p>
        The available actions are:
    </p>
    <ul>
        <li><code>Add</code> - adds a dependant item.</li>
        <li><code>Disable</code> - makes the dependant item disabled.</li>
        <li><code>Enable</code> - makes the dependant item enabled.</li>
        <li><code>Hide</code> - makes the dependant item invisible (notice that while invisible,
            the item may still be required, which may make it impossible to input a valid form
            data, whenever hiding a field make it optional or set its value).</li>
        <li><code>Remove</code> - removes a dependant item.</li>
        <li><code>SetOptional</code> - makes the dependant item optional.</li>
        <li><code>SetRequired</code> - makes the dependant item required.</li>
        <li><code>SetValue</code> - sets teh value of the dependant item to the <code>Value</code>
            property of the action.</li>
        <li><code>Show</code> - makes the dependant item visible.</li>
    </ul>
    <div class="Remark">
        <p>
            Keep in mind that circular dependencies between different items may be dangerous.
            Consider the following determinacy set:
        </p>
        <p>
            when item a equals 'a', it sets the value of item b to 'b'; if item b equals 'b',
            it sets the value of item a to 'c'; if item a equals 'c', it sets the value of item
            b to 'd'; if item b equals 'd', it sets the value of item a to 'a';
        </p>
        <p>
            When the first determinacy is fired, it changes the value of item b. The change
            on item b fires the second determinacy. The second determinacy changes the value
            of item a, which in turn fires the third determinacy. The change after the third
            determinacy fires the fourth one, and finally the change in the fourth one fires
            the first one. Such a code will loop forever.
        </p>
    </div>
</asp:Content>
